package net.zjitc.dao.Product.impl;

import net.zjitc.damain.Product.Product;
import net.zjitc.damain.category.Category;
import net.zjitc.dao.Product.ProductDao;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by 95729 on 2017/3/21.
 */
@Repository
public class ProductDaoImpl implements ProductDao{
    @Resource
    private SessionFactory sessionFactory;
    protected Session getSession(){
        Session session = sessionFactory.getCurrentSession();
        return session;
    }
    @Override
    public List<Product> findAll() {
        return getSession().createQuery(
                "from Product")
                .list();
    }

    @Override
    public Product findById(Integer pid) {
        return (Product) getSession().createQuery(
                "from  Product where pid=?")
                .setParameter(0,pid)
                .uniqueResult();
    }

    @Override
    public void Delete(Product product) {
        getSession().delete(product);
    }

    @Override
    public void edit(Category model) {
        getSession().save(model);
    }

    @Override
    public void save(Product model) {
        getSession().save(model);
    }

    @Override
    public List<Product> findHotProductu(int i) {
        return getSession().createQuery(
                "from Product where isHot=?")
                .setParameter(0,1)
                .setFirstResult(0)
                .setMaxResults(i)
                .list();
    }

    @Override
    public List<Product> findNewProductu(int i) {
        return getSession().createQuery(
                "from Product order by pdate desc")
                .setFirstResult(0)
                .setMaxResults(i)
                .list();
    }

}
